﻿import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'notification_logic.dart';

/// 提醒设置页面
class NotificationView extends GetView<NotificationLogic> {
  const NotificationView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF282528),
      appBar: AppBar(
        backgroundColor: const Color(0xFF282528),
        elevation: 0,
        leading: GestureDetector(
          onTap: () => Get.back(),
          child: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 20,
          ),
        ),
        title: const Text(
          '提醒设置',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(24.w),
        child: Column(
          children: [
            Container(
              decoration: BoxDecoration(
                color: const Color(0xFF2A2A2A),
                borderRadius: BorderRadius.circular(28.r),
              ),
              child: Column(
                children: [
                  _buildSwitchItem(
                    '信息提醒推送',
                    controller.messageNotification,
                    (value) {
                      controller.messageNotification.value = value;
                      controller.updateReminderSettings();
                    },
                  ),
                  _buildDivider(),
                  _buildSwitchItem(
                    '电池低电量提醒',
                    controller.batteryNotification,
                    (value) {
                      controller.batteryNotification.value = value;
                      controller.updateReminderSettings();
                    },
                  ),
                  _buildDivider(),
                  _buildSwitchItemWithSubtitle(
                    '运动心率预警',
                    '你的心率上限默认值为200BPM',
                    controller.heartRateAlert,
                    (value) {
                      controller.heartRateAlert.value = value;
                      controller.updateReminderSettings();
                    },
                  ),
                  _buildDivider(),
                  _buildSwitchItem(
                    '计划提醒',
                    controller.planReminder,
                    (value) {
                      controller.planReminder.value = value;
                      controller.updateReminderSettings();
                    },
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSwitchItem(
      String title, RxBool value, Function(bool) onChanged) {
    return Container(
      height: 60.h,
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: Row(
        children: [
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                color: Colors.white,
                fontSize: 16.sp,
              ),
            ),
          ),
          Obx(() => _buildIOSStyleSwitch(value.value, onChanged)),
        ],
      ),
    );
  }

  Widget _buildSwitchItemWithSubtitle(
      String title, String subtitle, RxBool value, Function(bool) onChanged) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 12.h),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16.sp,
                  ),
                ),
                SizedBox(height: 4.h),
                Text(
                  subtitle,
                  style: TextStyle(
                    color: const Color(0xFF666666),
                    fontSize: 12.sp,
                  ),
                ),
              ],
            ),
          ),
          Obx(() => _buildIOSStyleSwitch(value.value, onChanged)),
        ],
      ),
    );
  }

  /// iOS风格的开关（橙色）
  Widget _buildIOSStyleSwitch(bool value, Function(bool) onChanged) {
    return GestureDetector(
      onTap: () => onChanged(!value),
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        width: 51.w,
        height: 31.h,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15.5.r),
          color: value
              ? const Color(0xFFFF9500)
              : const Color(0xFF39393D), // 橙色或深灰色
        ),
        child: AnimatedAlign(
          duration: const Duration(milliseconds: 200),
          alignment: value ? Alignment.centerRight : Alignment.centerLeft,
          child: Container(
            width: 27.w,
            height: 27.h,
            margin: EdgeInsets.symmetric(horizontal: 2.w),
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withOpacity(0.3),
                  blurRadius: 4,
                  offset: const Offset(0, 2),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildDivider() {
    return Divider(
      height: 1,
      thickness: 1,
      color: const Color(0xFF1A1A1A),
      indent: 20.w,
      endIndent: 20.w,
    );
  }
}
